Scenario based problems
(Style - use of Procedures & Functions)


Objectives : Student should be able to -


Q1.  The names of patients are stored in the one-dimensional (1D) array Patient[ ] of type string. A separate two-dimensional (2D) array Readings[ ] stores the latest data recorded about each patient. The array already contains the readings taken by a nurse for each patient:

Temperature readings should be in the range 31.6 to 37.2 inclusive.
Pulse readings should be in the range 55.0 to 100.0 inclusive.

The hospital number given to the patient is used for the index on both arrays, this is a value between 1 and 1000 inclusive.

When the data for a patient is checked a warning is given if any of the readings are out of range. If both readings are out of range, then a severe warning is given.

Write a procedure, using pseudocode or program code, that meets the following requirements:

You must use pseudocode or program code and add comments to explain how your code works.

You do not need to initialise the data in the arrays.


'// Declaration of variables and constants.
CONSTANT  TempHigh ‹— 37.2
CONSTANT  TempLow ‹— 31.6
CONSTANT  PulseHigh ‹— 100
CONSTANT  PulseLow ‹— 55

'// Define the Procedure that takes the hospital number as parameter
PROCEDURE  CheckPatient(HospitalNumber : INTEGER)
'// Check for valid hospital number.
IF HospitalNumber >= 1 AND HospitalNumber <= 1000 THEN
OUTPUT "Name of Patient ", Patient(HospitalNumber)
'// check if all readings normal
IF Reading[HospitalNumber, 1] <= TempHigh AND
Reading[HospitalNumber, 1] >= TempLow AND
Reading[HospitalNumber, 2] <= PulseHigh AND
Reading[HospitalNumber, 2] >= PulseLow THEN
OUTPUT "Normal readings"
ENDIF
'// check if pulse out of range
IF (Reading[HospitalNumber, 1] <= TempHigh AND
Reading[HospitalNumber, 1] >= TempLow) AND
(Reading[HospitalNumber, 2] > PulseHigh OR
Reading[HospitalNumber, 2] < PulseLow) THEN
OUTPUT "Warning Pulse"
ENDIF
'// check if temp out of range
IF (Reading[HospitalNumber, 1] > TempHigh OR
Reading[HospitalNumber, 1] < TempLow) AND
(Reading[HospitalNumber, 2] <= PulseHigh AND
Reading[HospitalNumber, 2] >= PulseLow) THEN
OUTPUT "Warning temperature"
ENDIF
'// check if both out of range
IF (Reading[HospitalNumber, 1] > TempHigh OR
Reading[HospitalNumber, 1] < TempLow) AND
(Reading[HospitalNumber, 2] > PulseHigh OR
Reading[HospitalNumber, 2] < PulseLow) THEN
OUTPUT "Severe warning, Pulse and temperature"
ENDIF
ELSE
OUTPUT "Hospital number not valid"
ENDIF
END PROCEDURE

Q2.  An online Social media company stores the customer’s phone number in 1D array PhoneNo[ ]. The customer’s first name, last name and password in a 2D array UserProfile[ ].

The position of each customer’s data in the two arrays is the same, for example, the phone number in position 10 in PhoneNo[ ] and UserProfile[ ] is the same.

The variable CustomerNo contains the number of customers in the company.

The arrays and variables have already been set up and data stored.

Write an algorithm that meets the following requirements:

Perform the task for each option by calling an appropriately named procedure or function.


‘// Declare Global variables
DECLARE  LoginTag : STRING
DECLARE  CustomerIndex : INTEGER
'// Initialize a tag variable with value “No” to check if customer is logged-in or not.
LoginTag ‹— "No"

‘// Ask to enter the choice with validation
REPEAT
'// Call the PROCEDURE to display the options available to choose.
CALL  DisplayChoice

OUTPUT  “Enter your choice (option 1 to 3) = ”
INPUT  Choice

CASE  Choice  OFF
1 :   CALL  AccountLogin ‘// Call the PROCEDURE
2 :   CALL  ChangePassword ‘// Call the PROCEDURE
3 :   OUTPUT  “Thanks, see you again.”
OTHERWISE
OUTPUT   “Invalid, re-enter the choice.”
ENDCASE
UNTIL  Choice = 3 ‘// Exit the program for choice of option-3.

‘// Define PROCEDURE to display the options available to choose.
PROCEDURE  DisplayChoice
OUTPUT “1.   Login to the account.”
OUTPUT “2.   Change the password.”
OUTPUT “3.   Quit.”
END PROCEDURE

‘// Define the PROCEDURE to Login.
PROCEDURE  AccountLogin
‘// Check if the customer is already logged-in or not
IF LoginTag = “No” THEN
OUTPUT  “Enter the phone number : ”
INPUT  Phone
OUTPUT  “Enter the password : ”
INPUT  Pwd
‘// Search for matching phone number and password matches in array.
FOR  People = 1 TO CustomerNo
IF (PhoneNo[People] = Phone AND UserProfile[People, 3] = Pwd) THEN
OUTPUT  “Welcome ”, UserProfile[People, 1], “ ”, UserProfile[People, 2]
‘// Stores the index of the customer for later use
CustomerIndex = People
LoginTag = “Yes” ‘// Tag the Login variable as Yes
EXIT FOR ‘// Exit the loop, once the record is found.
ELSE
LoginTag = “No”
END IF
NEXT  People
‘// Output error message, if the record is not found
IF LoginTag = “No” THEN  OUTPUT  “Error, invalid phone or password.”
ELSE
OUTPUT  “You are already logged into the account.”
END IF
END PROCEDURE

‘// Define the PROCEDURE to change the password.
PROCEDURE  ChangePassword
‘// Check if customer is already logged-in or not.
IF  LoginTag = “Yes” THEN
OUTPUT  “Enter the new password : ”
INPUT  NewPwd
'// Change the password in array
UserProfile[CustomerIndex, 3] ‹— NewPwd
OUTPUT  “Your password is changed successfully.”
ELSE
OUTPUT  “Please, login to change the password.”
END IF
END PROCEDURE

Q3.  Write a program in pseudocode that uses a two-dimensional array, Game[ ] to store the moves in a "noughts and crosses" game.

The program should meet the following requirements:

Use procedures and parameters in your program.

You must use pseudocode code and add comment to explain how your code works.


'// Declare and Initialise the Array with empty space.
DECLARE  Game : ARRAY[1:3, 1:3] OF STRING

FOR  Row ‹— 1 TO 3
FOR  Col ‹— 1 TO 3
Game[Row, Col] ‹— ""
NEXT  Col
NEXT  Row

'// Loop to input 9 different moves for 3x3 cells.
FOR  Move ‹— 1 TO 9
'// Input the location of move and accept only if it is empty.
REPEAT
OUTPUT  "Enter the any row (1 to 3) : "
INPUT  XAxis
OUTPUT  "Enter the any column (1 to 3) : "
INPUT  YAxis
IF  NOT(Game[XAxis, YAxis] = "") THEN
OUTPUT  "Invalid, this place is not empty, re-enter your location."
ENDIF
UNTIL  Game[XAxis, YAxis] = ""

'// Input in turn to accept only “X” or “O”, and ensure that each player uses different symbol.
REPEAT
OUTPUT  "Put nought or cross in row-", XAxis, ", Col-", YAxis, " = "
INPUT  Symbol
'// Storing input in upper case letter
Play ‹— UPPER[Symbol]

'// Check and produce error message.
IF NOT(Play = ”X” OR Play = ”O”) THEN OUTPUT “Invalid symbol, try again”
IF (Move > 1 AND Play = SymbolTag) THEN OUTPUT “Invalid, put other symbol”

UNTIL  (Play = “X” OR Play = “O”) AND  NOT(Play = SymbolTag)

'// Tag the input to ensure that consequent inputs are not same.
SymbolTag ‹— Play

'// Store the input in ARRAY after validation
Game[XAxis, YAxis] ‹— Play

'// Call the Procedure by passing current input symbol as argument to output the winner.
Call  Result(Play)

NEXT  Move

'// Define the Procedure to get the current sysmbol as parameter value
PROCEDURE Result(NCMark : STRING)
'// Check for same mark in straight row
FOR Row ‹— 1 TO 3
FOR Col ‹— 1 TO 3
IF Game[Row, Col] = NCMark THEN
InRow ‹— "True"
ELSE
InRow ‹— "False"
ENDIF
'// Exit the inner-loop if two columns of the row are not same
IF  InRow = "False" THEN  EXIT FOR
NEXT Col
'// Exit the loop if all columns in a row are same
IF  InRow = "True" THEN  EXIT FOR
NEXT Row

'// Check for same mark in straight column
FOR Col ‹— 1 TO 3
FOR Row ‹— 1 TO 3
IF Game[Row, Col] = NCMark THEN
InCol ‹— "True"
ELSE
InCol ‹— "False"
ENDIF
'// Exit the inner-loop if two rows of the column are not same
IF  InCol = "False" THEN  EXIT FOR
NEXT Row
'// Exit the loop if all columns in a row are same
IF  InCol = "True" THEN  EXIT FOR
NEXT Col

'// Check for same mark in diagonal-1 (left to right)
FOR D1 ‹— 1 TO 3
IF Game[D1, D1] = NCMark THEN
InD1 ‹— "True"
ELSE
InD1 ‹— "False"
ENDIF
'// Exit the loop if two marks are not same
IF  InD1 = "False" THEN  EXIT FOR
NEXT D1

'// Check for same mark in diagonal-2 (right to left)
D2Col ‹— 3
FOR D2 ‹— 1 TO 3
IF Game[D2, D2Col] = NCMark THEN
InD2 ‹— "True"
ELSE
InD2 ‹— "False"
ENDIF
'// Decreasing the 2nd index of array by 1
D2Col ‹— D2Col - 1
'// Exit the loop if two marks are not same
IF  InD2 = "False" THEN  EXIT FOR
NEXT D1

'// Check and output if the marks are same in any straight line
IF (InRow = "True" OR InCol = "True OR InD1 = "True" OR InD2 = "True") THEN
OUTPUT  "Player with ”, NCMark, “-symbol has won the game."
ENDIF

END PROCEDURE




* * * * * * * * *
* * * * * *
* * *
*